package com.itheima.api.returnreason;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.returnreason.OmsOrderReturnApplyMapper;
import com.itheima.vo.pojo.OmsOrderReturnApply;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class OmsOrderReturnApplyServiceImpl
        extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply>
        implements OmsOrderReturnApplyService {

    @DubboReference
    private OmsOrderReturnApplyService applyService;

    @Autowired
    private OmsOrderReturnApplyMapper applyMapper;

    /**
     * 条件分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param id
     * @param createTime
     * @param handleMan
     * @param handleTime
     * @return
     */
    public Map findByPage(Integer pageNum, Integer pageSize, Integer id,
                          String createTime, String handleMan, String handleTime) {
        //创建分页构造器
        Page<OmsOrderReturnApply> page = new Page<>(pageNum, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, OmsOrderReturnApply::getId, id);
        lqw.eq(createTime != null, OmsOrderReturnApply::getCreateTime, createTime);
        lqw.eq(handleMan != null, OmsOrderReturnApply::getHandleMan, handleMan);
        lqw.eq(handleTime != null, OmsOrderReturnApply::getHandleTime, handleTime);
        applyService.page(page, lqw);
        Map map = new HashMap();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        map.put("totalPage", page.getPages());
        return map;
    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     */
    public void deleteByIds(List<Integer> ids) {
        //创建条件构造器
        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids != null, OmsOrderReturnApply::getId, ids);
        applyService.remove(lqw);
    }

    /**
     * 修改退货申请状态
     *
     * @param id
     * @param returnApply
     */
    public void updateStatus(Integer id, OmsOrderReturnApply returnApply) {
        //创建条件构造器
        LambdaUpdateWrapper<OmsOrderReturnApply> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(id != null, OmsOrderReturnApply::getId, id);
        applyService.update(returnApply, lqw);
    }


}
